package com.runa.monitor.platinterface.netservice.equipment;

import com.runa.persistence.util.SpringContextUtil;
import com.runa.monitor.platinterface.platService.NetEquService;
import com.runa.monitor.platinterface.base.BaseContainer;
import com.runa.monitor.platinterface.base.async.FutureDataTask;
import com.runa.monitor.platinterface.netservice.connect.Session;
import com.runa.monitor.platinterface.netservice.connect.TellSession;
import com.runa.monitor.platinterface.netservice.server.ListenerServer;
import com.runa.protocol.enums.DtuModel;
import com.runa.protocol.enums.ModbusType;

import java.util.Map;

/**
 * @author zhuj
 */
public class PublicDtuPlcService extends DtuPlcService {

    public PublicDtuPlcService(Integer id, int slaveId,
                               String dtuID, Integer limit, Integer timeout,
                               ModbusType modbusType, DtuModel dtuModel,
                               FutureDataTask<Map<Integer, String>> task, ListenerServer listener) {
        super(id, slaveId, dtuID, limit, timeout, modbusType, dtuModel, task);
        Session session = listener.getClientSession(dtuID);
        if (session == null) {
            session = new TellSession(getTimeout());
            ((TellSession) session).corrPlcService(this);
            listener.setClientSession(dtuID, session);
        }
        setSession(session);
    }

    @Override
    public void updateCommStatus(boolean status) {
        if (!status) {
            SpringContextUtil.getBean(NetEquService.class).removeInstance(getDtuID());
        }
        BaseContainer.netStatusCallback().rccStatusChange(getDtuID(), status);
    }

}
